package org.dromara.commission.controller;

import java.util.ArrayList;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.commission.domain.vo.LzshAgentLatitudeImportVo;
import org.dromara.commission.listener.LzshAgentLatitudeImportListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.commission.domain.vo.LzshAgentLatitudeVo;
import org.dromara.commission.domain.bo.LzshAgentLatitudeBo;
import org.dromara.commission.service.ILzshAgentLatitudeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 代理商纬度
 *
 * @author Lion Li
 * @date 2024-08-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/commission/agentLatitude")
public class LzshAgentLatitudeController extends BaseController {

    private final ILzshAgentLatitudeService lzshAgentLatitudeService;

    /**
     * 查询代理商纬度列表
     */
    @SaCheckPermission("commission:agentLatitude:list")
    @GetMapping("/list")
    public TableDataInfo<LzshAgentLatitudeVo> list(LzshAgentLatitudeBo bo, PageQuery pageQuery) {
        //如果不是超级管理员就只查找自己的
        if(!LoginHelper.isSuperAdmin()){
            String username = LoginHelper.getUsername();//获取商家的经营主体码businessCode
            bo.setBusinessCode(username);
        }
        return lzshAgentLatitudeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出代理商纬度列表
     */
    @SaCheckPermission("commission:agentLatitude:export")
    @Log(title = "代理商纬度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LzshAgentLatitudeBo bo, HttpServletResponse response) {
        //如果不是超级管理员就只导出自己的
        if(!LoginHelper.isSuperAdmin()){
            String username = LoginHelper.getUsername();//获取商家的经营主体码businessCode
            bo.setBusinessCode(username);
        }
        List<LzshAgentLatitudeVo> list = lzshAgentLatitudeService.queryList(bo);
        ExcelUtil.exportExcel(list, "代理商维度", LzshAgentLatitudeVo.class, response);
    }


    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "代理商维度管理", businessType = BusinessType.IMPORT)
    @SaCheckPermission("commission:agentLatitude::import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<LzshAgentLatitudeImportVo> result = ExcelUtil.importExcel(file.getInputStream(), LzshAgentLatitudeImportVo.class, new LzshAgentLatitudeImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "代理商维度数据", LzshAgentLatitudeImportVo.class, response);
    }
    /**
     * 获取代理商纬度详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("commission:agentLatitude:query")
    @GetMapping("/{id}")
    public R<LzshAgentLatitudeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(lzshAgentLatitudeService.queryById(id));
    }

    /**
     * 新增代理商纬度
     */
    @SaCheckPermission("commission:agentLatitude:add")
    @Log(title = "代理商纬度", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LzshAgentLatitudeBo bo) {
        return toAjax(lzshAgentLatitudeService.insertByBo(bo));
    }

    /**
     * 修改代理商纬度
     */
    @SaCheckPermission("commission:agentLatitude:edit")
    @Log(title = "代理商纬度", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LzshAgentLatitudeBo bo) {
        return toAjax(lzshAgentLatitudeService.updateByBo(bo));
    }

    /**
     * 删除代理商纬度
     *
     * @param ids 主键串
     */
    @SaCheckPermission("commission:agentLatitude:remove")
    @Log(title = "代理商纬度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(lzshAgentLatitudeService.deleteWithValidByIds(List.of(ids), true));
    }
}
